package com.zzwc.vote.www.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zzwc.vote.utils.R;
import com.zzwc.vote.www.entity.VoteEnterprise;
import com.zzwc.vote.www.entity.VoteUser;
import com.zzwc.vote.www.service.IVoteEnterpriseService;
import com.zzwc.vote.www.service.IVoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class StatisticsController {

    @Autowired
    private IVoteEnterpriseService voteEnterpriseService;

    @Autowired
    private IVoteUserService userService;

    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    public R statistics() {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<VoteEnterprise> wrapper = new EntityWrapper<>();
        //总票数
        Double ticketTotal = voteEnterpriseService.selectList(wrapper).stream().mapToDouble(VoteEnterprise::getCount).sum();
        map.put("ticketTotal", ticketTotal);
        //参与企业数
        int companyTotal = voteEnterpriseService.selectCount(wrapper);
        map.put("companyTotal", companyTotal);
        //参与人数
        EntityWrapper<VoteUser> userEntityWrapper = new EntityWrapper<>();
        int userTotal = userService.selectCount(userEntityWrapper);
        map.put("userTotal", userTotal);
        return R.ok().put("data", map);
    }
}
